package com.example.feidaolumanager.pages

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.example.feidaolumanager.R
import com.example.feidaolumanager.databinding.ActivityMainBinding
import com.example.feidaolumanager.viewModel.MainViewModel
import com.google.android.material.bottomnavigation.BottomNavigationView

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val navView: BottomNavigationView = binding.navView

        val navController = findNavController(R.id.nav_host_fragment_activity_main)
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.navigation_index, R.id.navigation_map, R.id.navigation_mine
            )
        )
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)

        val mainViewModel = ViewModelProvider(this)[MainViewModel::class.java]
        Log.e(this.toString(), "onCreate: ${mainViewModel.hashCode()}")

        mainViewModel.ifShowMenu.observe(this) {
            Log.e(this.toString(), "onCreate: $it")
            navView.visibility = if (it) {
                View.VISIBLE
            } else {
                View.GONE
            }
        }

        supportActionBar?.hide()
    }
}